package com.huangfu.learning.javabase.java.collections.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/7/21
 * Time: 14:45
 * Version: 1.0
 * Description:FastJson练习
 **/
public class FastJsonTest {
    public static void main(String[] args) {
        //生成json JavaBean、List<JavaBean>、List<String>、List<Map<String,Object>>
        Dog dog = new Dog("hali", "black", "2");
        Dog dog2 = new Dog("ami", "white", "3");
        Dog dog3 = new Dog("davi", "yellow", "4");
        List list = new ArrayList();
        list.add(dog);
        list.add(dog2);
        list.add(dog3);

        System.out.println("--------Object转成json--------------");
        String jsonString = JSON.toJSONString(dog);
        System.out.println(jsonString);
        System.out.println(jsonString);
        // 把json转化成其他类型
        System.out.println("--------json转成javabean---class要是public的-----------");
        dog = JSON.parseObject(jsonString, Dog.class);
        System.out.println(dog);

        System.out.println("--------json转成List<JavaBean>---class要是public的-----------");
        jsonString = JSON.toJSONString(list);
        System.out.println("转换之前的数据：" + jsonString);
        list = JSON.parseArray(jsonString, Dog.class);
        System.out.println(list);

        System.out.println("--------json转成List<String>--------------");
        List<String> listString = JSON.parseArray(jsonString, String.class);
        System.out.println(listString);

        System.out.println("--------json转成List<Map<String,Object>>-map格式不带双引号-------------");
        List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>() {
        });
        System.out.println(listMap);

        String text = "{\"totalRecords\":2615,  \n" +
                "\"result\":{\"code\":\"200\",\"status\":\"success\"},  \n" +
                "\"list\":[{\"unuAbnId\":\"0bcd930f-014c-1000-e003-5f160a0d0114\",  \n" +
                "\"entNo\":\"1c2e4ca8-00fa-1000-e000-74590a76bf0f\",  \n" +
                "\"regNO\":\"442000600169663\",  \n" +
                "\"entName\":\"x\",  \n" +
                "\"entType\":\"9910  \",  \n" +
                "\"speCause\":\"3\",  \n" +
                "\"abnTime\":\"Mar 13, 2015 12:00:00 AM\",  \n" +
                "\"decOrg\":\"442020\",  \n" +
                "\"entNameUrl\":\"<a href=\\\".. \",  \n" +
                "\"auditingFileNo\":\"15000684990326\",  \n" +
                "\"abnormalID\":\"fd74013d-014b-1000-e00a-72970a0d0114\"}],  \n" +
                "\"pageNo\":1,  \n" +
                "\"pageSize\":8,  \n" +
                "\"url\":\"main/abnInfoPage\",  \n" +
                "\"selList\":[{\"unuAbnId\":\"0bcd930f-014c-1000-e003-5f0f0a0d0114\",  \n" +
                "\"entNo\":\"16da9629-0131-1000-e005-3effc0a803a8\",  \n" +
                "\"regNO\":\"442000602187424\",  \n" +
                "\"entName\":\"x\",  \n" +
                "\"entType\":\"9910  \",  \n" +
                "\"speCause\":\"3\",  \n" +
                "\"abnTime\":\"Mar 13, 2015 12:00:00 AM\",  \n" +
                "\"decOrg\":\"442020\",  \n" +
                "\"entNameUrl\":\"<a href=\\\"..\\\">\",  \n" +
                "\"auditingFileNo\":\"15000684990319\",  \n" +
                "\"abnormalID\":\"fd74013d-014b-1000-e00a-72970a0d0114\"}],  \n" +
                "\"topPageNo\":1,  \n" +
                "\"totalPages\":327,  \n" +
                "\"previousPageNo\":0,  \n" +
                "\"nextPageNo\":2,  \n" +
                "\"bottomPageNo\":327  \n" +
                "}  ";
        JSONObject jsonObject = JSON.parseObject(text);
        JSONArray jsonArray = jsonObject.getJSONArray("selList");

        System.out.println("selList:" + jsonArray);
        System.out.println("-----------------获取jsonArray其中一个对象的元素-----------------------");
        for (int i = 0; i < jsonArray.size(); i++) {
            System.out.println(JSON.parseObject(jsonArray.get(i).toString()).get("regNO"));
        }
        jsonArray.forEach(json -> System.out.println(JSON.parseObject(json.toString()).get("regNO")));
    }
}


